<template>
  <div class="box">
    <!-- 头部区域 -->
    <div class="login">
      <h1>后台登录管理</h1>
      <!-- 表单区域 -->
      <el-form>
        <el-form-item>
          <el-input
            v-model="user.username"
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-input
            v-model="user.password"
            prefix-icon="el-icon-lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="doLogin" style="width: 100%"
            >立即登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 用户信息
      user: {
        username: "",
        password: ""
      }
    };
  },
  created() {},
  mounted() {},
  methods: {
    //获取用户登录接口
    doLogin() {
      //判断输入不为空
      if (this.user.username == "" || this.user.password == "") {
        // 错误信息提示
        this.$message.error("用户名后密码不能为空");
        return false;
      }
      // 发起接口登录请求
      this.$axios
        .post("https://www.liulongbin.top:8888/api/private/v1/login", this.user)
        .then(res => {
          console.log(res);
          // 如果status为200登录成功
          if (res.meta.status == 200) {
            // 成功后的判断
            this.$message.success(res.meta.msg);
            // 把登陆成功的信息保存到本地存储
            sessionStorage.setItem("03B_user", res.data.token);
            // 跳转到管理后台页面
            this.$router.push("/admin/home");
          } else {
            this.$message.error(res.meta.msg);
          }
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .login {
    width: 30%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 10px;
    border: 1px #ddd solid;
    border-radius: 5px;
    text-align: center;
    h1 {
      font-size: 40px;
      font-weight: bold;
    }
  }
}
</style>
